<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="68px">
      <el-row>
        <el-col :span="4">
          <el-form-item label="批次号" prop="batchNo">
            <el-input v-model="queryParams.batchNo" placeholder="请输入批次号" clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4" v-show="false">
          <el-form-item label="生产工单" prop="moId">
            <el-input v-model="queryParams.moId" placeholder="请输入生产排产ID" clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="生产工单" prop="moCode">
            <el-input v-model="queryParams.moCode" placeholder="请选择生产工单" disabled >
              <el-button slot="append" icon="el-icon-search" @click="openSelectSingleMoQuery = true"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4" v-show="false">
          <el-form-item label="生产计划" prop="planId">
            <el-input v-model="queryParams.planId" placeholder="请输入生产计划ID" clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="生产计划" prop="planCode">
            <el-input v-model="queryParams.planCode" placeholder="请选择生产计划" disabled >
              <el-button slot="append" icon="el-icon-search" @click="openSelectSinglePlan = true"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4" v-show="false">
          <el-form-item label="产品信息" prop="itemProduceId">
            <el-input v-model="queryParams.itemProduceId" placeholder="请选择产品信息" clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="产品信息" prop="itemCode">
            <el-input v-model="queryParams.itemCode" placeholder="请选择产品信息" disabled >
              <el-button slot="append" icon="el-icon-search" @click="openSelectItemProduceQuery = true"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="工厂" prop="factoryId">
            <selectFactory v-model="queryParams.factoryId"></selectFactory>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="生产车间" prop="workshopId">
            <selectWorkshop v-model="queryParams.workshopId"></selectWorkshop>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="工作中心" prop="centerId">
            <selectCenter v-model="queryParams.centerId"></selectCenter>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="部门" prop="deptId">
            <selectDept v-model="queryParams.deptId"></selectDept>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="流水号" prop="serialNo">
            <el-input v-model="queryParams.serialNo" placeholder="请输入流水号" clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="审核状态" prop="approve">
            <el-select v-model="queryParams.approve" placeholder="请选择审核状态" clearable size="small">
              <el-option v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_APPROVE)"
                         :key="dict.value" :label="dict.label" :value="dict.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                            range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['mes:report:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['mes:report:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" align="center" prop="id" fixed="left" />
      <el-table-column label="生产工单" align="center" prop="moCode" fixed="left" width="150px" />
      <el-table-column label="工厂" align="center" prop="factoryName" />
      <el-table-column label="生产车间" align="center" prop="workshopName" />
      <el-table-column label="工作中心" align="center" prop="centerName" />
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="批次号" align="center" prop="batchNo" width="150px" />
      <el-table-column label="生产计划" align="center" prop="planCode" width="150px" />
      <el-table-column label="流水号" align="center" prop="serialNo" width="150px" />
      <el-table-column label="报工工序" align="center" prop="processName" />
      <el-table-column label="报工数量" align="center" prop="reportAmount" />
      <el-table-column label="产品名称" align="center" prop="itemName" width="100px" />
      <el-table-column label="产品代码" align="center" prop="itemCode" width="100px" />
      <el-table-column label="产品规格" align="center" prop="spec" width="100px" show-overflow-tooltip />
      <el-table-column label="产品图号" align="center" prop="drawCode" width="100px" />
      <el-table-column label="报工人" align="center" prop="userName" show-overflow-tooltip />
      <el-table-column label="报工时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="80">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['mes:report:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产工单" prop="moCode">
              <el-input v-model="form.moCode" placeholder="请输入生产工单" :disabled="true">
                <el-button slot="append" icon="el-icon-search" @click="openSelectSingleMo = true"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报工人" prop="userId">
              <el-input :value="nickname" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="流水号" prop="serialNo">
              <el-input v-model="form.serialNo" placeholder="请输入流水号" @input="changSerialNoEvent" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报工数量" prop="reportAmount">
              <el-input type="number" v-model="form.reportAmount" :disabled="reportAmountInputDisable" placeholder="请输入报工数量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="报工工序" prop="processId">
              <el-select v-model="form.moRouteId" placeholder="请选择" @change="changeProcess">
                <div v-for="item in form.routes" >
                  <el-option :key="item.id" :label="item.processName" :value="item.id"></el-option>
                </div>
              </el-select>
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">-->
<!--            <el-form-item label="协作人" prop="cooperate">-->
<!--              <selectMultipleUser v-model="form.cooperate"></selectMultipleUser>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
        </el-row>
        <el-row v-show="form">
          <el-col :span="24">
            <el-divider content-position="left">生产信息</el-divider>
            <el-descriptions border>
              <el-descriptions-item label="工厂">{{ form.factoryName }}</el-descriptions-item>
              <el-descriptions-item label="生产车间">{{ form.workshopName }}</el-descriptions-item>
              <el-descriptions-item label="部门">{{ form.deptName }}</el-descriptions-item>
              <el-descriptions-item label="批次号">{{ form.batchNo }}</el-descriptions-item>
              <el-descriptions-item label="生产计划">{{ form.planCode }}</el-descriptions-item>
              <el-descriptions-item label="排产数量">{{ form.produceAmount }}</el-descriptions-item>
              <el-descriptions-item label="产品名称">{{ form.itemName }}</el-descriptions-item>
              <el-descriptions-item label="产品代码">{{ form.itemCode }}</el-descriptions-item>
              <el-descriptions-item label="产品图号">{{ form.drawCode }}</el-descriptions-item>
              <el-descriptions-item label="产品规格">{{ form.spec }}</el-descriptions-item>
            </el-descriptions>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-divider content-position="left">生产状态</el-divider>
            <el-steps :active="activeStep" align-center finish-status="success">
              <el-step v-for="(item, index) in form.routes" :key="item.id" :title="item.processName">
                <template slot="description">
                  <p>排产数量：{{form.produceAmount}}</p>
                  <p>报工数量：{{item.produceAmount}}</p>
                </template>
              </el-step>
            </el-steps>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查询产品生产信息(查询使用) -->
    <selectItemProduce :openSelectItemProduce.sync='openSelectItemProduceQuery' @selectItemProduce='selectItemProduceQuery'></selectItemProduce>
    <!-- 查询产品生产计划 -->
    <selectSinglePlan :openSelectSinglePlan.sync="openSelectSinglePlan" @selectSinglePlan="selectSinglePlan"></selectSinglePlan>
    <!-- 查询产品生产工单(查询使用) -->
    <selectSingleMo :openSelectSingleMo.sync="openSelectSingleMoQuery" @selectSingleMo="selectSingleMoQuery"></selectSingleMo>
    <!-- 查询产品生产工单（报工使用） -->
    <selectSingleMo :openSelectSingleMo.sync="openSelectSingleMo" @selectSingleMo="selectSingleMo"></selectSingleMo>
  </div>
</template>

<script>
import { createReport, updateReport, deleteReport, getReport, getReportPage, exportReportExcel } from "@/api/mes/report";
import { getMoInfo } from "@/api/mes/mo"
import SelectFactory from '@/components/SelectFactory/index.vue'
import SelectCenter from '@/components/SelectCenter/index.vue'
import SelectDept from '@/components/SelectDept/index.vue'
import SelectSinglePlan from '@/components/SelectSinglePlan/index.vue'
import SelectItemProduce from '@/components/SelectItemProduce/index.vue'
import SelectSingleMo from '@/components/SelectSingleMo/index.vue'
import SelectMultipleUser from '@/components/SelectMultipleUser/index.vue'
import SelectWorkshop from '@/components/SelectWorkshop/index.vue'
import { mapGetters } from 'vuex'

export default {
  name: "Report",
  components: {
    SelectFactory,
    SelectCenter,
    SelectDept,
    SelectSinglePlan,
    SelectItemProduce,
    SelectSingleMo,
    SelectMultipleUser,
    SelectWorkshop
  },
  computed: {
    ...mapGetters([
      'nickname',
    ]),
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 当前工艺
      activeStep: undefined,
      // 选择产品弹窗
      openSelectItemProduceQuery: false,
      // 打开选择生产计划
      openSelectSinglePlan: false,
      // 打开选择生产工单
      openSelectSingleMoQuery: false,
      // 打开选择生产工单
      openSelectSingleMo: false,
      // 报工数量输入框
      reportAmountInputDisable: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产报工列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        moId: null,
        moCode: null,
        planId: null,
        planCode: null,
        batchNo: null,
        itemId: null,
        itemCode: null,
        itemProduceId: null,
        factoryId: null,
        workshopId: null,
        centerId: null,
        deptId: null,
        userId: null,
        cooperate: null,
        processId: null,
        serialNo: null,
        approveTime: [],
        approveUser: null,
        approve: null,
        createTime: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        moId: [{ required: true, message: "生产排产ID不能为空", trigger: "blur" }],
        moRouteId: [{ required: true, message: "报工工序不能为空", trigger: "blur" }],
        reportAmount: [{ required: true, message: "报工数量不能为空", trigger: "blur" }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getReportPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        moId: undefined,
        cooperate: undefined,
        moRouteId: undefined,
        reportAmount: undefined,
        serialNo: undefined,
        reportType: 'batch',
      };
      this.resetForm("form");
    },
    /* 选择生产计划 */
    selectSinglePlan(data) {
      this.queryParams.planId = data.id;
      this.queryParams.planCode = data.code;
    },
    /** 选择产品生产信息回调 */
    selectItemProduceQuery(item) {
      this.queryParams.itemProduceId = item.id;
      this.queryParams.itemCode = item.itemCode;
    },
    /* 选择产品生产工单 */
    selectSingleMoQuery(data) {
      this.queryParams.moId = data.id;
      this.queryParams.moCode = data.code;
    },
    /* 选择产品生产工单 */
    selectSingleMo(data) {
      this.form.moCode = data.code
      this.getMoFun(data.id)
    },
    /* 查询生产工单 */
    getMoFun(id) {
      getMoInfo(id).then(response => {
        const data = response.data;
        const moId = data.id;
        const moCode = data.code;
        this.form = data;
        this.form.moId = moId;
        this.form.moCode = moCode;
        this.form.moRouteId = undefined;
        this.activeStep = undefined;
        this.form.reportType = 'batch';
      })
    },
    /* 选择工序事件 */
    changeProcess(row) {
      const routes = this.form.routes.filter(process => {
        return row == process.id;
      })
      this.activeStep = routes[0].subsequence-1;
    },
    /* 流水号选择事件 */
    changSerialNoEvent(data) {
      if (data.length == 0) {
        this.form.reportType = 'batch';
        this.reportAmountInputDisable = false;
      }else {
        this.form.reportType = 'serial';
        this.form.reportAmount = 1;
        this.reportAmountInputDisable = true;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产报工";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getReport(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产报工";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 添加的提交
        createReport(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除生产报工编号为"' + id + '"的数据项?').then(function() {
          return deleteReport(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有生产报工数据项?').then(() => {
          this.exportLoading = true;
          return exportReportExcel(params);
        }).then(response => {
          this.$download.excel(response, '生产报工.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
<style>
.title {
  font-size: 16px;
  font-weight: bold;
  margin: 20px 0px;
}
</style>
